#include "student.h"

// 录入学生信息
void add(struct prime_class *class_p)
{
    assert(class_p != NULL);

    int position;
    int stu_num = class_p->stu_num;

    if (stu_num >= SIZE_STU - 1) {
        printf("班级人数已满， 无法再添加\n");
        return ;
    }

    printf("请输入添加学生的位置[1-%d]：", stu_num + 1);
    scanf("%d", &position);

    if (position > class_p->stu_num + 1 || position < 1) {
        printf("位置错误\n");
        return ;
    }

    for (int i = stu_num; i >= position; i--) {
        class_p->stu[i + 1] = class_p->stu[i];
    }

    char input_id[SIZE_ID];
    printf("姓名: ");
    scanf("%s", class_p->stu[position].name);
    getchar();

    printf("学号: ");
    scanf("%s", input_id);
    getchar();
    
    for (int i = 1; i <= class_p->stu_num ; i++) {
        if (strcmp(class_p->stu[i].id, input_id) == 0) {
           printf("学生[%s]已存在.\n", input_id) ;
           return;
        }
    }

    strcpy(class_p->stu[position].id, input_id);

    printf("年龄: ");
    scanf("%d", &class_p->stu[position].age);
    getchar();

    printf("分数: ");
    scanf("%f", &class_p->stu[position].score);
    getchar();

    class_p->stu_num += 1;

    printf("新生录入完成!\n");
}

// 打印学生列表
void list(struct prime_class *class_p)
{
    printf("姓名\t学号\t年龄\t分数\n");

    for (int i = 1; i <= class_p->stu_num; i++) {
        printf("%s\t%s\t%d\t%.2f\n", 
                class_p->stu[i].name, 
                class_p->stu[i].id,
                class_p->stu[i].age,
                class_p->stu[i].score);
    }
}

// 删除学生信息
void del(struct prime_class *class_p)
{
    char stu_id[SIZE_ID];
    int stu_index = 0;
    int stu_num = class_p->stu_num;

    printf("输入学号删除学生信息:");
    printf("> ");
    scanf("%s", stu_id);

    for (int i = 1; i <= stu_num ; i++) {
        if (strcmp(class_p->stu[i].id, stu_id) == 0) {
           stu_index = i; 
        }
    }

    if (stu_index == 0) {
        printf("学生[%s]不存在\n", stu_id);
        return ;
    }

    strcpy(class_p->stu[stu_index].name, "");
    strcpy(class_p->stu[stu_index].id, "");
    class_p->stu[stu_index].age = 0;
    class_p->stu[stu_index].score = 0;


    for (int i = stu_index; i <= stu_num; i++) {
        if (i + 1 <= stu_num) {
            strcpy(class_p->stu[i].name, class_p->stu[i+1].name);
            strcpy(class_p->stu[i].id, class_p->stu[i+1].id);
            class_p->stu[i].age = class_p->stu[i+1].age;
            class_p->stu[i].score= class_p->stu[i+1].score;
        }
    }

    class_p->stu_num -= 1;

    printf("学生[%s]信息已删除!\n", stu_id);
}

// 更新学生信息
void update(struct prime_class *class_p)
{
    char stu_id[SIZE_ID];
    int stu_index = 0;

    printf("输入学号修改学生信息:");
    printf("> ");
    scanf("%s", stu_id);

    for (int i = 1; i <= class_p->stu_num ; i++) {
        if (strcmp(class_p->stu[i].id, stu_id) == 0) {
           stu_index = i; 
        }
    }

    if (stu_index == 0) {
        printf("学生[%s]不存在\n", stu_id);
        return ;
    }


    printf("姓名:");
    scanf("%s", class_p->stu[stu_index].name);
    getchar();

    printf("年龄: ");
    scanf("%d", &class_p->stu[stu_index].age);
    getchar();

    printf("分数: ");
    scanf("%f", &class_p->stu[stu_index].score);
    getchar();

    printf("学生[%s]信息编辑完成!\n", stu_id);

}

// 按分数排序
void sort(struct prime_class *class_p)
{
    int stu_num = class_p->stu_num;

    for (int i = 1; i <= stu_num; i++) {
        for (int j = 1; j <= stu_num - i; j++) {
            struct student tmp_stu;
            if (class_p->stu[j].score < class_p->stu[j+1].score) {
                strcpy(tmp_stu.name, class_p->stu[j].name);
                strcpy(tmp_stu.id, class_p->stu[j].id);
                tmp_stu.age = class_p->stu[j].age;
                tmp_stu.score = class_p->stu[j].score;

                strcpy(class_p->stu[j].name, class_p->stu[j+1].name);
                strcpy(class_p->stu[j].id, class_p->stu[j+1].id);
                class_p->stu[j].age = class_p->stu[j+1].age;
                class_p->stu[j].score = class_p->stu[j+1].score;

                strcpy(class_p->stu[j+1].name, tmp_stu.name);
                strcpy(class_p->stu[j+1].id, tmp_stu.id);
                class_p->stu[j+1].age = tmp_stu.age;
                class_p->stu[j+1].score = tmp_stu.score;
            }
        }
    }


    printf("姓名\t学号\t年龄\t分数\n");

    for (int i = 1; i <= class_p->stu_num; i++) {
        printf("%s\t%s\t%d\t%.2f\n", 
                class_p->stu[i].name, 
                class_p->stu[i].id,
                class_p->stu[i].age,
                class_p->stu[i].score);    
    }

}
